package cm.dpassyann.speedDating.ui.activity.start.activity;

import roboguice.activity.RoboActivity;
import roboguice.activity.event.OnCreateEvent;
import roboguice.activity.event.OnResumeEvent;
import roboguice.activity.event.OnStopEvent;
import roboguice.event.EventManager;
import roboguice.event.Observes;
import roboguice.inject.ContextSingleton;
import roboguice.inject.InjectView;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ProgressBar;
import android.widget.TextSwitcher;
import cm.dpassyann.speedDating.R;
import cm.dpassyann.speedDating.model.dao.user.IUserDao;
import cm.dpassyann.speedDating.ui.activity.ILifeCycleActivity;
import cm.dpassyann.speedDating.ui.activity.connection.activity.ConnectionActivity;
import cm.dpassyann.speedDating.ui.activity.start.IStartActivity;
import cm.dpassyann.speedDating.ui.activity.start.task.StartActivityTask;
import cm.dpassyann.speedDating.ui.event.OnSpeedDatingExceptionEvent;
import cm.dpassyann.speedDating.util.SpeedDatingConstant;

import com.google.inject.Inject;

/**
 * @author deungoueyann
 */
@ContextSingleton
public class StartActivity extends RoboActivity implements IStartActivity, ILifeCycleActivity {

	/***********************************************************************************************************************
	 *                                           DÉCLARATION ET INJECTION DES VARIABLES                                    *
	 ***********************************************************************************************************************/
	@Inject IUserDao listUserDaoMock;
	@InjectView(R.id.progressBar1) ProgressBar progressBar;
	@InjectView(R.id.textSwitcher1) TextSwitcher textSwitcher;
	@InjectView(R.id.loaderTextSwitcher) TextSwitcher loaderTextSwitcher;
	@Inject StartActivityTask startActivityTask;
	@Inject EventManager eventManager;

	/**
	 * @variable permettant de savoir si la tâche en fond est finie
	 */
	private boolean _isFinishTask;
	/***********************************************************************************************************************
	 *                                             FONCTIONNEMENT GÉNÉRAL DE L'ACTIVITÉ                                    *
	 ***********************************************************************************************************************/
	public void showNextCopyrightText() {
		textSwitcher.showNext();
	}

	public void initProgress(int max) {
		progressBar.setMax(max);
	}

	public void resetProgress() {
		progressBar.setMax(0);
	}

	public void updateProgress(int value) {
		progressBar.setProgress(value);
	}

	public void showNextLoadingText(){
		loaderTextSwitcher.showNext();
	}

	public boolean finishTask() {
		_isFinishTask = true;
		eventManager.fire(new OnResumeEvent());
		return _isFinishTask;
	}


	/***********************************************************************************************************************
	 *                                           GESTION DES CONFIGURATION DE L'ACTIVITÉ                                   *
	 ***********************************************************************************************************************/

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		//on teste la configuration

		/**
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			Log.w("****£"+StartActivity.class+"£*******","LANDSCAPE");
		} 

		else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			Log.w("****£"+StartActivity.class+"£*******","PORTAIT");
		}

		//*/

		//si besoin je peux forcer le refresh de la vue ici (locale|keyboardHidden|navigation|orientation|screenSize)
		//screensize aussi utilisé si on vérouille l'écran

		/** 
		setContentView(R.layout.main);
		//*/
	}

	@SuppressWarnings("deprecation")
	@Override
	public Object onRetainNonConfigurationInstance() {
		//méthode permettant de sauvegarder les configuration de la vue
		//Object o = getLastNonConfigurationInstance(); pour rétupérer les dernières config save
		return super.onRetainNonConfigurationInstance();
	}

	/************************************************************************************************************************************************************
	 *                                               CYCLE DE VIE DE L'ACTIVITÉ                                                                                 *
	 ************************************************************************************************************************************************************/

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}


	public void onCreateActivity(@Observes OnCreateEvent onCreate) {
		setContentView(R.layout.main);
		Animation in = AnimationUtils.loadAnimation(this,  android.R.anim.fade_in);
		Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);

		textSwitcher.setInAnimation(in);
		textSwitcher.setOutAnimation(out);

		in = AnimationUtils.loadAnimation(this,  android.R.anim.slide_in_left);
		out = AnimationUtils.loadAnimation(this,  android.R.anim.slide_out_right);

		loaderTextSwitcher.setInAnimation(in);
		loaderTextSwitcher.setOutAnimation(out);

		//progressBar.animate();
		//Toast.makeText(getBaseContext(), listUserDaoMock.getUserNames().get(0).toLowerCase(), Toast.LENGTH_LONG).show();
	}

	public void onResumeActivity(@Observes OnResumeEvent onResume) {
		Log.w("****£"+StartActivity.class+"£*******","Called doSomethingOnResume in onResume");
		if(_isFinishTask){
			_isFinishTask = false;
			switchView();
		}
		else{
			startActivityTask.execute(90);
		}
	}

	public void onStopActivity(@Observes OnStopEvent onStop) {
		Log.w("****%$$%*******","Called doSomethingOnResume in onStop");
		SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key_start_activity), Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPref.edit();
		String login = getString(R.string.key_login_preference);
		String pwd = getString(R.string.key_password_preference);

		if(login == null || pwd == null || login.equals("") || pwd.equals("")){
			editor.putInt(getString(R.string.key_lap_preference), getResources().getInteger(R.integer.default_lap_preference));
			editor.commit();
		}
	}
	
	public void onErrorActivity(@Observes OnSpeedDatingExceptionEvent event) {
		
	}

	/**
	 * Méthode permettant
	 */
	private void switchView() {
		SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key_start_activity), Context.MODE_PRIVATE);
		int initLap = getResources().getInteger(R.integer.default_lap_preference);
		int lap = sharedPref.getInt(getString(R.string.key_lap_preference), initLap);
		Intent intent = new Intent(StartActivity.this,ConnectionActivity.class);
		startActivityTask.cancel(true);
		switch (lap) {
		case 1:
			intent.putExtra(SpeedDatingConstant.CONNECTION_PARAMETERS, false);
			startActivity(intent);
			finish();
			break;

		default:
			//			intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			intent.putExtra(SpeedDatingConstant.CONNECTION_PARAMETERS, true);
			startActivity(intent);
			finish();
			break;
		}	
	}
}